﻿@model Sheng.Enterprise.Web.Models.UserDetailViewModel

@{
    ViewBag.Title = "人员详情";
    Layout = "~/Views/Shared/_Layout.cshtml";

    ViewBag.LeftTitle = "设 置";
    ViewBag.MainMenu = "Settings";
    ViewBag.LeftMenu = "User";

}

<script>

    var _edit = false;
    var _validator;


    $(document).ready(function () {
        _validator = $("#formUser").validate({
            rules: {
                "txtAccount": {
                    required: true,
                    minlength: 3
                },
                "txtName": "required"
            },
            messages: {
                "txtAccount": "!",
                "txtName": "!"
            }
        });

        //设置部门，默认设置为组织机构的根目录
        setSelectedOrganization("organizationSelector", '@ViewBag.RootOrganization.Id', '@ViewBag.RootOrganization.Name');

        loadUser();

        _edit = "@(Model.User != null)" === "True";

        if (_edit) {
            $("#spanMessage").html("");

            $("#btnRemove").show();
            $("#btnResetPassword").show();

            $("#divContainerTitle").html('@(Model.User!=null?Model.User.Name:"人员")');
        }
        else {
            $("#spanMessage").html("新增人员后初始密码为：123");

            $("#btnRemove").hide();
            $("#btnResetPassword").hide();
        }

        $("[keyenter]").keypress(function (e) {
            if (e.keyCode == 13) {
                save();
            }
        });
    });

    function loadUser() {
        @if (Model.User != null)
        {
            <text>
        $("#txtId").val('@Model.User.Id');
        $("#txtAccount").val('@Model.User.Account');
        //$("#txtNumber").val('@Model.User.Number');
        $("#txtName").val('@Model.User.Name');
        setSelectedOrganization("organizationSelector", '@Model.User.OrganizationId', '@Model.User.OrganizationName');
        $("#selectJobTitle").val('@Model.User.JobTitleId');
        $("#selectJobLevel").val('@Model.User.JobLevelId');
        $("#selectOfficeLocation").val('@Model.User.OfficeLocationId');
        $("#txtExtTelphone").val('@Model.User.ExtTelphone');
        $("#txtTelphone").val('@Model.User.Telphone');
        $("#txtCellphone").val('@Model.User.Cellphone');
        $("#txtEmail").val('@Model.User.Email');
        $("#selectNotify").val('@Model.User.Notify');

        $(".chkWorkType").each(function (n, value) {

        });

        </text>

           
            }

            @foreach (var item in Model.UserWorkTypeList)
        {
            <text>
    $(".chkWorkType[id='@item.WorkType']").attr("checked","checked");
            </text>
        }
    }

    function save() {
        if (_validator.form() === false) {
            layerInputAlertMsg();
            return;
        }

        var user = new Object();

        if (_edit) {
            user.Id = $("#txtId").val();
        }
        user.DomainId = '@ViewBag.Domain.Id';
        user.OrganizationId = getSelectedOrganization("organizationSelector").Id;
        user.Account = $("#txtAccount").val();
        //user.Number = $("#txtNumber").val();
        user.Name = $("#txtName").val();
        user.JobTitleId = $("#selectJobTitle").find("option:selected").val();
        user.JobLevelId = $("#selectJobLevel").find("option:selected").val();
        user.OfficeLocationId = $("#selectOfficeLocation").find("option:selected").val();
        user.Email = $("#txtEmail").val();
        user.ExtTelphone = $("#txtExtTelphone").val();
        user.Telphone = $("#txtTelphone").val();
        user.Cellphone = $("#txtCellphone").val();
        user.Notify = $("#selectNotify").val();

        var workTypeArray = new Array();
        $(".chkWorkType:checked").each(function (n, value) {
            workTypeArray[n] = $(value).attr("id");
        });

        var apiUrl = "/Api/User/Update";

        if (_edit === false) {
            apiUrl = "/Api/User/Create";
        }

        var userDto = new Object();
        userDto.User = user;
        userDto.WorkTypeList = workTypeArray;

        showMask();
        var layerIndex = layer.load(0, {
            shade: false
        });

        $.ajax({
            url: apiUrl,
            type: "POST",
            dataType: "json",
            data: JSON.stringify(userDto),
            success: function (data, status, jqXHR) {
                layer.closeAll();
                hideMask();

                if (data.Success) {
                    window.location.href = "/Settings/User";

                    // _validator.resetForm();
                } else {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest) {
                layer.closeAll();
                hideMask();

                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function resetPassword() {
        layer.confirm("是否确定重置该用户的密码？", { title: false, closeBtn: false, shift: _layerShift }, function (index) {
            layer.close(index);

            showMask();
            var layerIndex = layer.load(0, {
                shade: false
            });

            $.ajax({
                url: "/Api/User/ResetPasswordToDefault?id=" + $("#txtId").val(),
                type: "POST",
                dataType: "json",
                success: function (data, status, jqXHR) {
                    layer.closeAll();
                    hideMask();

                    if (data.Success) {
                        layer.msg("该用户的密码已重置为：123。", {
                            time: 2000
                        });
                    } else {
                        layerAlert(data.Message);
                    }
                },
                error: function (xmlHttpRequest) {
                    layer.closeAll();
                    hideMask();

                    alert("Error: " + xmlHttpRequest.status);
                }
            });
        });
    }

    function removeData() {
        layer.confirm("是否确定删除该用户？", { title: false, closeBtn: false, shift: _layerShift }, function (index) {
            layer.close(index);

            showMask();
            var layerIndex = layer.load(0, {
                shade: false
            });

            $.ajax({
                url: "/Api/User/Remove?id=" + $("#txtId").val(),
                type: "POST",
                dataType: "json",
                success: function (data, status, jqXHR) {
                    layer.closeAll();
                    hideMask();

                    if (data.Success) {
                        window.location.href = "/Settings/User";
                    } else {
                        layerAlert(data.Message);
                    }
                },
                error: function (xmlHttpRequest) {
                    layer.closeAll();
                    hideMask();

                    alert("Error: " + xmlHttpRequest.status);
                }
            });
        });
    }



</script>


<div id="divContent">
    <div id="divContainerTitle">人员</div>
    <form id="formUser">
        <div style="position:absolute; top:70px; bottom:0px;left:20px;right:20px;OVERFLOW-Y: auto;">
            <input type="hidden" id="txtId" />
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="100" height="50">账号：</td>
                    <td><input name="txtAccount" type="text" class="input_18" style="width:350px;" id="txtAccount" keyenter /> *</td>
                </tr>
                <tr>
                    <td width="100" height="30">&nbsp;</td>
                    <td valign="top" class="font_gray_15">请输入3位以上账号。</td>
                </tr>
                @*<tr>
                        <td width="100" height="30">&nbsp;</td>
                        <td><span class="font_gray_15" style="">5位以上，推荐使用电子邮件地址或手机号码等唯一性较强的标识作为帐户</span></td>
                    </tr>*@
                @*<tr>
                        <td width="100" height="50">工号：</td>
                        <td><input name="txtNumber" type="text" class="input_18" style="width:350px;" id="txtNumber" keyenter /></td>
                    </tr>*@
                <tr>
                    <td height="50">姓名：</td>
                    <td><input name="txtName" type="text" class="input_18" style="width:350px;" id="txtName" keyenter /> *</td>
                </tr>
                <tr>
                    <td height="50">部门：</td>
                    <td>
                        @Helpers.OrganizationSelector("organizationSelector", null, null, ViewBag.RootOrganization.Id, ViewBag.RootOrganization.Name)
                    </td>
                </tr>
                <tr>
                    <td height="50">职位：</td>
                    <td>
                        <select name="selectJobTitle" id="selectJobTitle" class="input_18" style="width:364px;" keyenter>
                            <option value="00000000-0000-0000-0000-000000000000"></option>
                            @foreach (var item in Model.JobTitleList)
                            {
                                <option value="@item.Id">@item.Name</option>
                            }
                        </select>
                    </td>
                </tr>
                <tr>
                    <td height="50">职级：</td>
                    <td>
                        <select name="selectJobLevel" id="selectJobLevel" class="input_18" style="width:364px;" keyenter>
                            <option value="00000000-0000-0000-0000-000000000000"></option>
                            @foreach (var item in Model.JobLevelList)
                            {
                                <option value="@item.Id">@item.Name</option>
                            }
                        </select>
                    </td>
                </tr>
                <tr>
                    <td height="50">办公地点：</td>
                    <td>
                        <select name="selectOfficeLocation" id="selectOfficeLocation" class="input_18" style="width:364px;" keyenter>
                            <option value="00000000-0000-0000-0000-000000000000"></option>
                            @foreach (var item in Model.OfficeLocationList)
                            {
                                <option value="@item.Id">@item.Name</option>
                            }
                        </select>
                    </td>
                </tr>
               
                <tr>
                        <td height="50">分机：</td>
                        <td><input name="txtExtTelphone" type="text" class="input_18" style="width:350px;" id="txtExtTelphone" keyenter /></td>
                    </tr>
                    <tr>
                        <td height="50">联系电话：</td>
                        <td><input name="txtTelphone" type="text" class="input_18" style="width:350px;" id="txtTelphone" keyenter /></td>
                    </tr>
                    <tr>
                        <td height="50">手机：</td>
                        <td><input name="txtCellphone" type="text" class="input_18" style="width:350px;" id="txtCellphone" /></td>
                    </tr>
                    <tr>
                        <td height="50">电子邮件：</td>
                        <td><input name="txtEmail" type="text" class="input_18" style="width:350px;" id="txtEmail" keyenter /></td>
                    </tr>
                    <tr>
                        <td height="30">&nbsp;</td>
                        <td><span class="font_gray_15" style="">请输入真实有效的电子邮件地址，否则将无法使用找回密码功能</span></td>
                    </tr>
                <tr>
                    <td height="50">是否提醒：</td>
                    <td>
                        <select name="selectNotify" id="selectNotify" class="input_18" style="width:364px;" keyenter>
                            <option value="False">否</option>
                            <option value="True">是</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td width="100" height="30">&nbsp;</td>
                    <td valign="top" class="font_gray_15">进入系统时是否提醒填写周报。</td>
                </tr>
                <tr>
                    <td height="50" valign="top">所属项目：</td>
                    <td>
                        <div>
                            @foreach (var item in Model.WorkTypeList)
                            {
                                <div><input id="@item.Id" type="checkbox" class="chkWorkType" /><label for="@item.Id">@item.Name</label> </div>
                            }
                        </div>
                    </td>
                </tr>
                <tr>
                    <td width="100" height="30">&nbsp;</td>
                    <td valign="top" class="font_gray_15">只能填写所属项目的周报；在按项目查询时，只能查询所属项目的周报。</td>
                </tr>
            </table>
            <table width="50%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>
                        <span class="font_gray_15" style="" id="spanMessage"></span>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
            </table>
            <table width="500" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td height="50"><input name="btnReturn" type="button" class="btn_blue" id="btnReturn" value="返 回" onclick="goUrl('@Url.Action("User")')" /><input name="btnSave" type="button" class="btn_blue" id="btnSave" value="保 存" style="margin-left:1px" onclick="save()" /><input name="btnResetPassword" type="button" class="btn_blue" id="btnResetPassword" value="重置密码" style="margin-left:1px" onclick="resetPassword()" /><input name="btnRemove" type="button" class="btn_red" id="btnRemove" value="删 除" style="margin-left:70px;" onclick="removeData()" /></td>
                </tr>

            </table>




        </div>
    </form>
</div>
